package com.project.springboot.controller;

import com.project.springboot.utils.QRCodeUtil;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

/**
 * description: 二维码相关API
 * @author 朱正磊
 * @date 2023-11-30 13:50:34
 */
@RestController
@RequestMapping("/qr/code/image")
public class QRCodeController {

    /**
     * 请求地址：<a href="http://127.0.0.1:8080/qr/code/image/generateQRCode">...</a>
     * description: 生成二维码图片
     * @author 朱正磊
     * @date 2023-11-30 13:53:20
     * @param response HTTP响应对象
     */
    @SneakyThrows
    @GetMapping("/generateQRCode")
    public void generateQRCode(HttpServletResponse response) {
        ServletOutputStream outputStream = response.getOutputStream();
        BufferedImage bufferedImage = QRCodeUtil
                .getQRCodeImage("1727138588721725447", "连包中药库-药架编号：1-2-3");
        response.setContentType("image/png");
        ImageIO.write(bufferedImage, "png", outputStream);
        // 在写完输出流之后一定要记得关闭输出流
        outputStream.flush();
        outputStream.close();
    }

}
